package com.fuhe.chen.vendingmachine.controller.admin;

import com.fuhe.chen.vendingmachine.constant.MessageConstant;
import com.fuhe.chen.vendingmachine.dto.ApiResponse;
import com.fuhe.chen.vendingmachine.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 用户管理首页
 */
@Api(tags = "用户管理")
@Controller
@RequestMapping("/admin/user")
public class UserController {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);

    public static final String ROLE_USER = "user";

    @Autowired
    IUserService userService;


    /**
     * 为用户赋权
     * @param role
     * @param user_id
     * @return
     */
    @ResponseBody
    @PostMapping("/empower")
    @ApiOperation("为用户赋权")
    public ApiResponse<String> empower(@RequestParam("role") String role,
                                       @RequestParam("user_id") String user_id) {
        ApiResponse<String> response = new ApiResponse<>();
        String message = userService.empower(user_id, role);

        response.setSuccess(true);
        response.setCode(10000);
        response.setMessage(message);

        return response;
    }


    /**
     * 为供货商账户分配机器
     */
    @ResponseBody
    @ApiOperation("为供货商账户分配机器")
    @PostMapping("/distributeMachine")
    public ApiResponse<String> distributeMachine(
            @RequestParam("machine_id") Integer machine_id,
            @RequestParam("supplier_id") String supplier_id) {
        ApiResponse<String> response = new ApiResponse<>();

        String message = userService.distributeMachine(supplier_id, machine_id);

        if(!message.equals(MessageConstant.Success.DO_SUCCESS_MESSAGE)){
            response.setSuccess(false);
            response.setCode(MessageConstant.Error.INVALID_PARAM_CODE);
            response.setMessage(message);
            return response;
        }
        response.setSuccess(true);
        response.setCode(10000);
        response.setMessage(message);
        return response;
    }

    /**
     * 收回供货商和机器的绑定关系
     */
    @ResponseBody
    @ApiOperation("收回供货商和机器的绑定关系")
    @PostMapping("/takeBackMachine")
    public ApiResponse<String> takeBackMachine(
            @RequestParam("relationship_id") Integer relationship_id) {
        ApiResponse<String> response = new ApiResponse<>();

        userService.takeBackMachine(relationship_id);

        response.setSuccess(true);
        response.setCode(10000);
        response.setMessage("操作成功");

        return response;
    }


}
